class RelativeDateFormat {
  //转成孕周
  static String pregnancyFormat(int starTime, int endTime) {
    int pregnant_time = endTime - starTime;
    int week_time = 7 * 24 * 60 * 60 * 1000;
    int day_time = 24 * 60 * 60 * 1000;
    int week_timeAll = 24796800000;
    String str_result = "";
    if (pregnant_time > week_timeAll) {
        str_result = "(已生产)";
    } else {
        int weeks = (pregnant_time / week_time).toInt();
        int remained_time = (pregnant_time % week_time);
        int days = (remained_time / day_time).toInt();
        if(days == 0){
            str_result = "(孕" + weeks.toString() + "周)";
        }else{
            str_result = "(孕" + weeks.toString() + "周" + days.toString() + "天)";
        }
    }
    return str_result;
  }
  
  //时间转时间戳
  static int timeStampFormat(String time) {
    if (time.length > 0) {
      return DateTime.parse(time).millisecondsSinceEpoch;
    }
    return 0;
  }
  
  //时间戳转年月日时分 2020-10-17 14:00
  static String YMDHMformat(int timestamp) {
    String time = DateTime.fromMillisecondsSinceEpoch(timestamp).toString();
    return time.substring(0,16);
  }
  //时间戳转年月日 2020-01-17
  static String YMDformat(int timestamp) {
    String time = DateTime.fromMillisecondsSinceEpoch(timestamp).toString();
    return time.substring(0,10);
  }

  //时间戳转月日 01-17
  static String MDformat(int timestamp) {
    String time = DateTime.fromMillisecondsSinceEpoch(timestamp).toString();
    time = time.substring(5,10);
    time = time.replaceAll("-", "月");
    return time + "日";
  }

  //时间戳转具体时间 14:00
  static String HMformat(int timestamp) {
    String time = DateTime.fromMillisecondsSinceEpoch(timestamp).toString();
    return time.substring(11,16);
  }

  static final num ONE_MINUTE = 60000;
  static final num ONE_HOUR = 3600000;
  static final num ONE_DAY = 86400000;
  static final num ONE_WEEK = 604800000;
 
  static final String ONE_SECOND_AGO = "秒前";
  static final String ONE_MINUTE_AGO = "分钟前";
  static final String ONE_HOUR_AGO = "小时前";
  static final String ONE_DAY_AGO = "天前";
  static final String ONE_MONTH_AGO = "月前";
  static final String ONE_YEAR_AGO = "年前";
 
  //时间转换
  static String format(DateTime date) {
    num delta = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
    if (delta < 1 * ONE_MINUTE) {
      num seconds = toSeconds(delta);
      return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
    }
    if (delta < 60 * ONE_MINUTE) {
      num minutes = toMinutes(delta);
      return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
    }
    if (delta < 24 * ONE_HOUR) {
      num hours = toHours(delta);
      return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
    }
    if (delta < 48 * ONE_HOUR) {
      return "昨天";
    }
    if (delta < 30 * ONE_DAY) {
      return YMDformat(date.millisecondsSinceEpoch);
    }
    if (delta < 12 * 4 * ONE_WEEK) {
      return YMDformat(date.millisecondsSinceEpoch);
    } else {
      return YMDformat(date.millisecondsSinceEpoch);
    }
  }
 
  static num toSeconds(num date) {
    return date / 1000;
  }
 
  static num toMinutes(num date) {
    return toSeconds(date) / 60;
  }
 
  static num toHours(num date) {
    return toMinutes(date) / 60;
  }
 
  static num toDays(num date) {
    return toHours(date) / 24;
  }
 
  static num toMonths(num date) {
    return toDays(date) / 30;
  }
 
  static num toYears(num date) {
    return toMonths(date) / 365;
  }
}


